/**
 * Created by a1 on 16/9/20.
 */
var testData = '{"title":"HTML 教程","content":"在本教程中,你将学习如何使用 HTML 来创建站点。 HTML 很容易学习!你会喜欢它的! 现在开始学习 HTML!HTML 实例 学习","date":"2016-01-01","imgUrl":"http://img.xiami.net/images/collect_pic/11-15/1447524372_mtmI_4.jpg"}'
//function PageDetailData() {
    var nodeContentImage
    var nodeContentTitle
    var nodeContentSource
    var nodeContentDetail
    var nodeContentAction
    var nodeContentRecommend
    var nodeContentRecommendList
//}
var detailData = JSON.parse(testData)
function initDetail() {
    nodeContentImage = $("#dm_detail_top_img")
    nodeContentTitle = $("#dm_detail_content_title")
    nodeContentSource = $("#dm_detail_content_source")
    nodeContentDetail = $("#dm_detail_content_detail")
    nodeContentAction = $("#dm_detail_content_action")
    nodeContentRecommendList = $("#dm_detail_content_recommend_list")
    nodeContentRecommend = $("#dm_detail_content_recommend")
    nodeContentRecommend.hide()

    initWithData()
}

function onShowDetail() {
    nodeContentImage = $("#dm_detail_top_img")
    nodeContentTitle = $("#dm_detail_content_title")
    nodeContentSource = $("#dm_detail_content_source")
    nodeContentDetail = $("#dm_detail_content_detail")
    nodeContentAction = $("#dm_detail_content_action")
    nodeContentRecommend = $("#dm_detail_content_recommend")
    nodeContentRecommendList = $("#dm_detail_content_recommend_list")
}

function initWithData() {
    if(!isEmpty(detailData[kImageUrl])) {
        nodeContentImage.attr("src", detailData[kImageUrl])
    }
    nodeContentSource.html("发表时间：" + detailData[kDate] + "&nbsp;&nbsp;&nbsp;&nbsp;来源：" + detailData["source"])
    nodeContentDetail.html(detailData[kContent])
    displayActions()
    displayRecommend()
}

function clear() {
    //nodeListContent.scrollTop(0)
    nodeContentRecommend.hide()
    nodeContentRecommendList.html("")
    nodeContentAction.html("")
}

function displayActions() {
    var data = {"fundName":"银华货币A","info":"银华货币A,天天有收益","actions":[{"type":"1","name":"申购"},{"type":"2","name":"定投"}]}
    for(var i = 0; i < 3; ++i) {
        addListActionItem(nodeContentAction, data)
    }
}

function addListActionItem(parent, data) {
    var item = $("<table class='dm_item_action'></table>")
    var content = $("<tr></tr>")
    var containerLeft = $("<td class='dm_item_action_container'></td>")
    content.append(containerLeft)
    var labelTitle = $("<div class='dm_item_action_name'>"+data["fundName"]+"</div>")
    containerLeft.append(labelTitle)
    var labelContent = $("<div class='dm_item_action_info'>"+data["info"]+"</div>")
    containerLeft.append(labelContent)

    var containerRight = $("<td class='dm_item_action_container'></td>")
    content.append(containerRight)
    var actions = data["actions"]
    for(var i = 0; i < actions.length; ++i) {
        var button = $("<a class='dm_item_action_button'>"+actions[i]["name"]+"</a>")
        containerRight.append(button)
    }
    item.append(content)
    parent.append(item)
}

function displayRecommend() {
    nodeContentRecommend.show()
    for(var i = 0; i < 3; ++i) {
        detailData[kID] = "" + i
        console.log("add id = " + detailData[kID])
        var labelDate = addListItem(nodeContentRecommendList, detailData, "2")
        labelDate.html("阅读:"+18)
    }
}
